﻿hpxAdminApp.controller('checkFBankBillController', function (exportService, $rootScope, $interval, $scope, $stateParams, $filter, $state, API_URL, NgTableParams, fBankService, billService, constantsService, addressService, EXPORT_URL) {
    $scope.filter = {
        bill_status: '1',
    };
    //获取电票汇票瑕疵信息
    constantsService.queryConstantsType(19).then(function (data) {
        $scope.billFlawData = data;
    });
    //获取承兑机构类型信息
    constantsService.queryConstantsType(4).then(function (data) {
        $scope.acceptorTypeData = data;
    })
    //获取所有未审核或者已审核的票据资料信息
    $scope.tableParams = new NgTableParams({'sorting': {'publishing_time': 'desc'}}, {
        getData: function (params) {
            return fBankService.getBillListForCheck(params, $scope.filter).then(function (data) {
                $scope.first = $scope.getFirst(params);
                return data;
            });
        }
    });
    //跳转到新页面，放大图片
    $scope.showFront = function () {
        window.open('index.html#/img?path=' + $scope.model.bill_front_photo_path);
    }
    $scope.showBack = function () {
        window.open('index.html#/img?path=' + $scope.model.bill_back_photo_path);
    }
    //刷新
    $scope.reflash = function () {
        $scope.tableParams.reload();
    }


    var timer = $interval($scope.reflash, 60 * 1000);
    $scope.$on(
        "$destroy",
        function (event) {
            $interval.cancel(timer);
        }
    );

    //设置瑕疵
    $scope.initBillFlaw = function () {
        for (var i = 0; i < $scope.billFlawData.length; i++) {
            $scope.billFlawData[i].checked = false;
        }
        for (var i = 0; i < $scope.model.billFlaw.length; i++) {
            for (var j = 0; j < $scope.billFlawData.length; j++) {
                if ($scope.model.billFlaw[i] == $scope.billFlawData[j].code) {
                    $scope.billFlawData[j].checked = true;
                }
            }
        }
    }
    //设置承兑机构
    $scope.initBillAccept = function () {
        for (var i = 0; i < $scope.acceptorTypeData.length; i++) {
            $scope.acceptorTypeData[i].checked = false;
        }
        $scope.model.bill_acceptor_id = [];
        for (var i = 0; i < $scope.acceptorTypeData.length; i++) {
            $scope.model.bill_acceptor_id.push($scope.acceptorTypeData[i].code)
        }
        function indexOf(arr, str) {
            if (arr && arr.indexOf) {
                return arr.indexOf(str);
            }
            var len = arr.length;
            for (var i = 0; i < len; i++) {
                if (arr[i] == str) {
                    return i;
                }
            }
            return -1;
        }
        $scope.model.bill_acceptor_index = indexOf($scope.model.bill_acceptor_id, $scope.model.acceptor_type_id);
        if ($scope.model.bill_acceptor_index != -1) {
            $scope.acceptorTypeData[$scope.model.bill_acceptor_index].checked = true;
        }
    }
    


    //点击审核/重新审核
    $scope.check = function (item) {
        $scope.model = item;         //获取某条客户资料详细信息
        $scope.initBillAccept();     //承兑机构
        //弹出审核窗口
        $('#modal-check').modal('show');    
        $('.jqzoom').imagezoom();
    };
    // 修改
    $scope.updateCheck = function (item) {
        $scope.model = item;         //获取某条客户资料详细信息
        $('#modal-updateCheck').modal('show');
        $('.jqzoom').imagezoom();
    }
    $scope.saveUpload = function () {
        $scope.model.createTime = $filter('date')($scope.model.createTime, 'yyyy-MM-dd');
        $scope.model.discountedDate = $filter('date')($scope.model.discountedDate, 'yyyy-MM-dd');
        $scope.model.dueDate = $filter('date')($scope.model.dueDate, 'yyyy-MM-dd');
        //$scope.model.billFlaw = [];
        //for (var i = 0; i < $scope.billFlawData.length; i++) {
        //    if ($scope.billFlawData[i].checked) {
        //        $scope.model.billFlaw.push($scope.billFlawData[i].code);
        //    }
        //}
        //审核票据产品，并推送审核结果给后台
        fBankService.fBankTrade($scope.model).then(function (data) {
            alert('修改成功！');
            $scope.tableParams.reload();
            $scope.checkForm.$setPristine();
            $('#modal-updateCheck').modal('hide');
        });
    }
    //点击详细
    $scope.resd = function (item) {
        $scope.model = item;         //获取某条客户资料详细信息
        console.log(item)
        $('#modal-read').modal('show');
        $('.jqzoom').imagezoom();       //图片放大功能
    };
    $scope.pass = function () {
        if (!$scope.model.acceptor_type_id) {
            alert("请选择承兑方类型");
            return;
        }
        if (confirm('确认通过吗？')) {
            $scope.model.isCheck = 1;
            save();
        }
    };
    $scope.reject = function () {
        if (!$scope.model.check_description || $scope.model.check_description.length == 0) {
            alert('请填写不通过原因！');
            return;
        }

        if (confirm('确认不通过吗？')) {
            $scope.model.isCheck = 2;
            save();
        }
    };

    save = function () {
        $scope.model.createTime = $filter('date')($scope.model.createTime, 'yyyy-MM-dd');
        $scope.model.discountedDate = $filter('date')($scope.model.discountedDate, 'yyyy-MM-dd');
        $scope.model.dueDate = $filter('date')($scope.model.dueDate, 'yyyy-MM-dd');
        //$scope.model.billFlaw = [];
        //for (var i = 0; i < $scope.billFlawData.length; i++) {
        //    if ($scope.billFlawData[i].checked) {
        //        $scope.model.billFlaw.push($scope.billFlawData[i].code);
        //    }
        //}
        //审核票据产品，并推送审核结果给后台
        fBankService.fBankTrade($scope.model).then(function (data) {
            $scope.tableParams.reload();
            //window.location.reload();
            $scope.checkForm.$setPristine();
            $('#modal-check').modal('hide');
        });
    };
    //电票，当选中无瑕疵时，其他选项均为false；反之，选中其他选项时，无瑕疵选项为false
    $scope.billFlawChange = function (item) {
        if (item.code == 1500) {
            item.checked = true;
            for (var i = 1; i < $scope.billFlawData.length; i++) {
                $scope.billFlawData[i].checked = false;
            }
        }
        else {
            for (var i = 0; i < $scope.billFlawData.length; i++) {
                if (i == 0) {
                    $scope.billFlawData[i].checked = true;
                }
                else {
                    if ($scope.billFlawData[i].checked) {
                        $scope.billFlawData[0].checked = false;
                    }
                }
            }
        }
    }
    //选择承兑机构类型
    $scope.acceptorTypeChange = function (item) {
        if (item.checked) {
            $scope.model.acceptor_type_id = item.code;
        }
    }

});
